package main

import (
	log "github.com/sirupsen/logrus"
	"gopkg.in/yaml.v3"
	"os"
	"publiclib/cache"
	"publiclib/config"
	"publiclib/dbutil"
)

var (
	confFile string
)

// loadConfFromFile 从配置文件加载配置信息
func loadConfFromFile(fs string, po interface{}) error {
	data, err := os.ReadFile(fs)
	if err == nil {
		if err = yaml.Unmarshal(data, po); err != nil {
			return err
		}
	}
	return nil
}

// DoInitLibs 初始基库
func DoInitLibs() {
	cfg := config.Instance()
	if err := loadConfFromFile(confFile, cfg); err != nil {
		panic(err)
	}

	log.Infof("redis uri %s:%s", cfg.Redis.GetURI(), cfg.Redis.Pass)
	// 初始Cache, 默认5秒, db3
	cache.Init(cfg.Redis.GetURI(), cfg.Redis.Pass, 5000, 0)

	// 初始Mysql, 只func只需调用一次
	if _, err := dbutil.InitMysql(cfg); err != nil {
		log.Panic("dbutil init err," + err.Error())
	}
}
